package com.toutiao.cut.service;

import org.apache.thrift.TException;

import com.toutiao.cut.SemanticCutter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public class MultiDocumentProcessImpl implements MultiDocumentProcess.Iface {
	
	private static final Logger _log = Logger.getLogger(MultiDocumentProcessImpl.class);
	
	private SemanticCutter cut;
	
	public MultiDocumentProcessImpl(SemanticCutter cut) {
		this.cut = cut;		
	}
	
    private String process_cut(String line) {
        StringBuilder buffer = new StringBuilder();
        List<String> ns = cut.process(line);
        for (String n : ns){
            buffer.append(n).append(" ");
        }
        return buffer.toString().trim();
    }
    
    private Map<String,String> process_doc(Map<String,String> data) {
    	
    	long st = System.nanoTime();
    	
    	Map<String,String> res = new HashMap<String,String>();
    	
    	String i = "";
    	if (data.containsKey("id")) {
    		i = data.get("id");
    	}
    	else if (data.containsKey("gid")) {
    		i = data.get("gid");
    	}
    	String r = "";
    	if (data.containsKey("req_name")){
    		r = data.get("req_name");
    	}
    	
    	res.put("id", i);
    	
    	try {
    		String title = "";
    		if (data.containsKey("title")){
    			title = data.get("title");
    		}
    		String content = "";
    		if (data.containsKey("txt_content")){
    			content = data.get("txt_content");
    			int cc = 10000;
    			if (content.length() > cc) {
    				content = content.substring(0,cc);
    			}
    		}
    		String source = "";
    		if (data.containsKey("source")) {
    			source = data.get("source");
    		}

            String cut_title = process_cut(title);
            String[] split_content = content.split("\n");
            StringBuilder cut_content = new StringBuilder();
            for (String line: split_content){
                cut_content.append(process_cut(line)).append("\n");
            }   
            String cut_source = process_cut(source);
            
            res.put("cut_title",cut_title);
            res.put("cut_content",cut_content.toString().trim());       
            res.put("cut_source", cut_source);
    		
    	}
    	catch (Exception e) {
    		_log.error(e.getMessage());
    	
    		res.put("cut_title", "");
    		res.put("cut_content", "");
    		res.put("cut_source", "");
    	}
    	
        long et = System.nanoTime();
        _log.info(r+" "+i+" "+(et-st)/1e6+"ms");     	
    	
    	return res;
    	
    }

	@Override
	public List<Map<String, String>> process(List<Map<String, String>> data) throws TException {
		// TODO Auto-generated method stub
		
		long st = System.nanoTime();
		
		List<Map<String,String>> res = new ArrayList<Map<String,String>>();
		for(Map<String,String> doc : data) {
			Map<String,String> r = process_doc(doc);
			res.add(r);
		}
		
		long et = System.nanoTime();
		
		_log.info("process time "+(et-st)/1e6+"ms");
		
		return res;
	}

}
